<template>
	<uni-card :is-shadow="false">
		<uni-section title="设备名称" type="line">
			<text class="">{{deviceName}}</text>
		</uni-section>
		<uni-section title="设备地址" type="line">
			<text class="">{{btInfo}}</text>
		</uni-section>
		<uni-section title="连接状态" type="line">
			<text class="">{{connectinfo}}</text>
		</uni-section>
		<uni-section title="传输合计" type="line">
			<text class="">{{ttx+', '+trx}}</text>
		</uni-section>
		<uni-section title="传输速度" type="line">
			<text class="">{{sprx+', '+sptx}}</text>
		</uni-section>
	</uni-card>
	<uni-card :is-shadow="false">
		<uni-section title="第一步" type="line" padding>
			<uni-easyinput v-model="deviceName" placeholder="称重设备名称"></uni-easyinput>
			<uni-easyinput v-model="deviceMac" placeholder="称重设备MAC地址"></uni-easyinput>
			<uni-easyinput v-model="btType" placeholder="设备类型"></uni-easyinput>
			<button @click="connectBluetooth" type="primary" class="mt-1 mx-1">连接蓝牙</button>
			<button @click="connectBluetooth" type="primary" class="mt-1 mx-1">重连</button>
		</uni-section>
		<uni-section title="数据区" type="line">
			<textarea v-model="msg_text"></textarea>
		</uni-section>
	</uni-card>
</template>

<script>
	export default {
		data() {
			return {
				deviceMac:'98:DA:F0:00:B5:4B',
				deviceName:'',
				btInfo:'设备地址，RSSI',
				btType:'[BLE]',
				msg_text:'',
				ttx:'TX: 0',
				trx:'TX: 0',
				sptx:'TX: 0',
				sprx:'RX: 0',
				sdk:null,
				connectinfo:'',
				
			}
		},
		onLoad() {
			this.sdk = uni.requireNativePlugin('Huo15-TtModule')
			this.sdk.startOnload()
			//通知
			const globalEvent = uni.requireNativePlugin('globalEvent')
			globalEvent.addEventListener('huo15_info', (e) => {
				console.log(JSON.stringify(e));
				if (e.device_info) {
					this.btInfo=e.device_info
				}
				if (e.connectinfo) {
					this.connectinfo=e.connectinfo
				}
				if (e.trx) {
					this.trx=e.trx
				}
				if (e.ttx) {
					this.ttx=e.ttx
				}
				if (e.sprx) {
					this.sprx=e.sprx
				}
				if (e.sptx) {
					this.sptx=e.sptx
				}
				if (e.msg_text) {
					this.msg_text=e.msg_text
				}
			
			})
		},
		methods: {
			connectBluetooth(){
				this.sdk.connectBluetooth({
					deviceMac:this.deviceMac,
					btType:this.btType,
				})
			}
		}
	}
</script>

<style>

</style>
